package com.bhs.sansong.ui.order;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.OrderGoodsAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.OrderGoodsDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 订单列表
 */
public class OrderGoodsListFragment extends BaseFragment implements NetCallBack {

    private String orderType;//发货单or收货单
    private String orderStatus;//订单状态
    private SwipeRefreshLayout mRefreshLayout;
    private OrderGoodsAdapter mAdapter;
    private RecyclerView mRvList;
    private int page = 1;
    private OrderOperation orderOperation;//列表操作按钮

    public static OrderGoodsListFragment newInstance(String orderType, String orderStatus) {
        OrderGoodsListFragment fragment = new OrderGoodsListFragment();
        Bundle args = new Bundle();
        args.putString("type", orderType);
        args.putString("orderStatus", orderStatus);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_order_goods_list;
    }

    @Override
    public void initView(View root) {
        if (getArguments() != null) {
            orderType = getArguments().getString("type");
            orderStatus = getArguments().getString("orderStatus");
        }
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        orderOperation = new OrderOperation((BaseActivity) getActivity());
    }

    @Override
    public void initData() {
        mAdapter = new OrderGoodsAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        orderOperation.operation(mAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        loadData();
    }

    Map<String, String> map = new ArrayMap<>();

    private void loadData() {
        map.put("search_type", orderType);
        if (orderStatus.equals("0")) {
            map.put("pay_status", "0");
        } else if (orderStatus.equals("101")) {
            map.put("with_receipt", "2");
        } else {
            map.put("status", orderStatus);
        }
        map.put("page", String.valueOf(page));
        networkRequest(netApi.get_order_list(map), this);
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderGoodsDataBean bean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}